package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    EditText mEtUserName;
    TextView mTvMsg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mEtUserName = findViewById(R.id.et_username);

    }

    public void Login(View view) {
        //保存 输入的数据到本地文件中
        String username = mEtUserName.getText().toString();
        try {
            FileOutputStream fileOutputStream=openFileOutput("1.txt",MODE_APPEND);
            byte[]msg = username.getBytes();
            fileOutputStream.write(msg);
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void read(View view) {
        try {
            FileInputStream fileInputStream = new FileInputStream("1.txt");
            byte[] bytes=new byte[fileInputStream.available()];
            fileInputStream.read(bytes);
            String msg = new String(bytes);
            //显示到
            mTvMsg.setText(msg);
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
